﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="frmSplitTable.cs" company="">
//   
// </copyright>
// <summary>
//   Defines the frmSplitTable type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AutoBackupDatabase.Form
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

    using AutoBackupDatabase.BLL;
    using AutoBackupDatabase.ENT;
    using AutoBackupDatabase.General;

    using DevExpress.Utils;

    public partial class FrmJoinTable : DevExpress.XtraEditors.XtraForm
    {
        private List<AREAENT> listAreaENT;

        private TABLEBLL tablebll;

        private AREABLL areabll;

        private List<EXPORTDETAILENT> listExp;

        private EXPORTBLL exportbll;
        public FrmJoinTable()
        {
            InitializeComponent();
            listAreaENT = new List<AREAENT>();
            tablebll = new TABLEBLL();
            areabll = new AREABLL();
            listExp = new List<EXPORTDETAILENT>();
           exportbll=new EXPORTBLL();
        }


        private void frmSplitTable_Load(object sender, EventArgs e)
        {
            listAreaENT = areabll.GetAllareaData();
            glkAreaNew.Properties.DataSource = listAreaENT;
            glkAreaNew.Properties.DisplayMember = "AREANAME";
            glkAreaNew.Properties.ValueMember = "AREAID";

            glkAreaOld.Properties.DataSource = listAreaENT;
            glkAreaOld.Properties.DisplayMember = "AREANAME";
            glkAreaOld.Properties.ValueMember = "AREAID";



        }

        private void glkAreaOld_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                glkTableOld.Properties.DataSource = tablebll.GETTABLEHAVEGUESTBYAREAID(glkAreaOld.EditValue.ToString());
                glkTableOld.Properties.DisplayMember = "TABLENAME";
                glkTableOld.Properties.ValueMember = "TABLEID";
            }
            catch (Exception)
            {

                return;
            }

        }

        private void glkAreaNew_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                glkTableNew.Properties.DataSource =
                    tablebll.GETTABLEHAVEGUESTBYAREAID(glkAreaNew.EditValue.ToString());
                glkTableNew.Properties.DisplayMember = "TABLENAME";
                glkTableNew.Properties.ValueMember = "TABLEID";
            }
            catch (Exception)
            {

                return;
            }
        }

        private void glkTableOld_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                this.listFlag.Clear();
                gridListItemNew.RefreshDataSource();

                EXPORTDETAILBLL bll = new EXPORTDETAILBLL();
                listExp = bll.GETDETAILEXPORTBYTABLEID(int.Parse(glkTableOld.EditValue.ToString()));
                gridListItemOld.DataSource = listExp;
                gridListItemOld.RefreshDataSource();
                if (gridView3.RowCount > 0)
                {
                    gridView3.FocusedRowHandle = gridView3.RowCount - 1;
                    gridView3.Focus();

                }
            }
            catch (Exception)
            {

                return;
            }
        }

        private List<EXPORTDETAILENT> listFlag = new List<EXPORTDETAILENT>();

        private int quantityPart = 0;

        private int quantityPart1 = 0;

        private void gridView3_DoubleClick(object sender, EventArgs e)
        {
            if ((e as DXMouseEventArgs).Button == System.Windows.Forms.MouseButtons.Left
                && (e as DXMouseEventArgs).Clicks == 2)
            {
                if (gridView3.RowCount == 1)
                {
                    if (int.Parse(gridView3.GetFocusedRowCellValue("QUANTITY").ToString()) > 1)
                    {
                        var itemFindNewList =
                            this.listFlag.Find(
                                f => f.ITEMID == int.Parse(gridView3.GetFocusedRowCellValue("ITEMID").ToString()));

                        if (itemFindNewList == null)
                        {
                            var resultOld =
                                this.listExp.Find(
                                    f => f.ITEMID == int.Parse(gridView3.GetFocusedRowCellValue("ITEMID").ToString()));

                            var newRecord = new EXPORTDETAILENT();
                            newRecord.AREANAME = resultOld.AREANAME;
                            newRecord.DATECREATE = resultOld.DATECREATE;
                            newRecord.DATEPAID = resultOld.DATEPAID;
                            newRecord.EMPLOYERID = resultOld.EMPLOYERID;
                            newRecord.EMPLOYERNAME = resultOld.EMPLOYERNAME;
                            newRecord.EXPORTID = resultOld.EXPORTID;
                            newRecord.ISITEMPAY = resultOld.ISITEMPAY;
                            newRecord.ITEMDISCOUNT = resultOld.ITEMDISCOUNT;
                            newRecord.ITEMID = resultOld.ITEMID;
                            newRecord.ITEMNAME = resultOld.ITEMNAME;
                            newRecord.ISITEMPAY = resultOld.ISITEMPAY;
                            newRecord.PRICE = resultOld.PRICE;
                            newRecord.QUANTITY = 1;
                            newRecord.SERVICE = resultOld.SERVICE;
                            newRecord.TABLEID = resultOld.TABLEID;
                            newRecord.TABLENAME = resultOld.TABLENAME;
                            newRecord.TOTAL = resultOld.TOTAL;
                            newRecord.VAT = resultOld.VAT;
                            listFlag.Add(newRecord);
                            gridListItemNew.DataSource = listFlag;
                            gridListItemNew.RefreshDataSource();
                            if (resultOld.QUANTITY == 1)
                            {
                                gridView3.DeleteRow(gridView3.GetFocusedDataSourceRowIndex());
                                return;
                            }
                            resultOld.QUANTITY--;
                            gridListItemOld.RefreshDataSource();
                            return;
                        }
                        var resultOld1 =
                            this.listExp.Find(
                                f => f.ITEMID == int.Parse(gridView3.GetFocusedRowCellValue("ITEMID").ToString()));
                        itemFindNewList.QUANTITY++;
                        gridListItemNew.DataSource = listFlag;
                        gridListItemNew.RefreshDataSource();
                        if (resultOld1.QUANTITY == 1)
                        {
                            gridView3.DeleteRow(gridView3.GetFocusedDataSourceRowIndex());
                            return;
                        }
                        resultOld1.QUANTITY--;
                        gridListItemOld.RefreshDataSource();
                        return;

                    }
                    else
                    {
                        MessageBox.Show("Không thể chuyển hết món ăn qua bàn mới vui lòng kiểm tra lại", "Thông báo");
                        return;
                    }
                }


                if (gridView3.RowCount >= 1)
                {

                    var itemFindNewList =
                        this.listFlag.Find(
                            f => f.ITEMID == int.Parse(gridView3.GetFocusedRowCellValue("ITEMID").ToString()));

                    if (itemFindNewList == null)
                    {
                        var resultOld =
                            this.listExp.Find(
                                f => f.ITEMID == int.Parse(gridView3.GetFocusedRowCellValue("ITEMID").ToString()));

                        var newRecord = new EXPORTDETAILENT();
                        newRecord.AREANAME = resultOld.AREANAME;
                        newRecord.DATECREATE = resultOld.DATECREATE;
                        newRecord.DATEPAID = resultOld.DATEPAID;
                        newRecord.EMPLOYERID = resultOld.EMPLOYERID;
                        newRecord.EMPLOYERNAME = resultOld.EMPLOYERNAME;
                        newRecord.EXPORTID = resultOld.EXPORTID;
                        newRecord.ISITEMPAY = resultOld.ISITEMPAY;
                        newRecord.ITEMDISCOUNT = resultOld.ITEMDISCOUNT;
                        newRecord.ITEMID = resultOld.ITEMID;
                        newRecord.ITEMNAME = resultOld.ITEMNAME;
                        newRecord.ISITEMPAY = resultOld.ISITEMPAY;
                        newRecord.PRICE = resultOld.PRICE;
                        newRecord.QUANTITY = 1;
                        newRecord.SERVICE = resultOld.SERVICE;
                        newRecord.TABLEID = resultOld.TABLEID;
                        newRecord.TABLENAME = resultOld.TABLENAME;
                        newRecord.TOTAL = resultOld.TOTAL;
                        newRecord.VAT = resultOld.VAT;
                        listFlag.Add(newRecord);
                        gridListItemNew.DataSource = listFlag;
                        gridListItemNew.RefreshDataSource();
                        if (resultOld.QUANTITY == 1)
                        {
                            gridView3.DeleteRow(gridView3.GetFocusedDataSourceRowIndex());
                            return;
                        }
                        resultOld.QUANTITY--;
                        gridListItemOld.RefreshDataSource();
                        return;
                    }
                    var resultOld1 =
                        this.listExp.Find(
                            f => f.ITEMID == int.Parse(gridView3.GetFocusedRowCellValue("ITEMID").ToString()));
                    itemFindNewList.QUANTITY++;
                    gridListItemNew.DataSource = listFlag;
                    gridListItemNew.RefreshDataSource();
                    if (resultOld1.QUANTITY == 1)
                    {
                        gridView3.DeleteRow(gridView3.GetFocusedDataSourceRowIndex());
                        return;
                    }
                    resultOld1.QUANTITY--;
                    gridListItemOld.RefreshDataSource();
                    return;
                }
            }

            if ((e as DXMouseEventArgs).Button == System.Windows.Forms.MouseButtons.Right
                && (e as DXMouseEventArgs).Clicks == 2)
            {

                var itemFindNewList =
                    this.listFlag.Find(
                        f => f.ITEMID == int.Parse(gridView3.GetFocusedRowCellValue("ITEMID").ToString()));

                if (itemFindNewList == null)
                {

                    return;
                }
                var resultOld1 =
                    this.listExp.Find(f => f.ITEMID == int.Parse(gridView3.GetFocusedRowCellValue("ITEMID").ToString()));
                if (resultOld1 != null)
                {
                    
                    if (itemFindNewList.QUANTITY == 1)
                    {
                        gridView5.DeleteRow(gridView5.GetFocusedDataSourceRowIndex());
                        gridListItemNew.RefreshDataSource();
                        resultOld1.QUANTITY++;
                       
                        //var newRecord1 = new EXPORTDETAILENT();
                        //newRecord1.AREANAME = itemFindNewList.AREANAME;
                        //newRecord1.DATECREATE = itemFindNewList.DATECREATE;
                        //newRecord1.DATEPAID = itemFindNewList.DATEPAID;
                        //newRecord1.EMPLOYERID = itemFindNewList.EMPLOYERID;
                        //newRecord1.EMPLOYERNAME = itemFindNewList.EMPLOYERNAME;
                        //newRecord1.EXPORTID = itemFindNewList.EXPORTID;
                        //newRecord1.ISITEMPAY = itemFindNewList.ISITEMPAY;
                        //newRecord1.ITEMDISCOUNT = itemFindNewList.ITEMDISCOUNT;
                        //newRecord1.ITEMID = itemFindNewList.ITEMID;
                        //newRecord1.ITEMNAME = itemFindNewList.ITEMNAME;
                        //newRecord1.ISITEMPAY = itemFindNewList.ISITEMPAY;
                        //newRecord1.PRICE = itemFindNewList.PRICE;
                        //newRecord1.QUANTITY = 1;
                        //newRecord1.SERVICE = itemFindNewList.SERVICE;
                        //newRecord1.TABLEID = itemFindNewList.TABLEID;
                        //newRecord1.TABLENAME = itemFindNewList.TABLENAME;
                        //newRecord1.TOTAL = itemFindNewList.TOTAL;
                        //newRecord1.VAT = itemFindNewList.VAT;

                        //listExp.Add(newRecord1);
                        //gridListItemOld.RefreshDataSource();
                        return;
                    }
                    itemFindNewList.QUANTITY--;
                    gridListItemNew.DataSource = listFlag;
                    gridListItemNew.RefreshDataSource();
                    resultOld1.QUANTITY++;
                    gridListItemOld.RefreshDataSource();
                    return;
                }
                var newRecord = new EXPORTDETAILENT();
                newRecord.AREANAME = itemFindNewList.AREANAME;
                newRecord.DATECREATE = itemFindNewList.DATECREATE;
                newRecord.DATEPAID = itemFindNewList.DATEPAID;
                newRecord.EMPLOYERID = itemFindNewList.EMPLOYERID;
                newRecord.EMPLOYERNAME = itemFindNewList.EMPLOYERNAME;
                newRecord.EXPORTID = itemFindNewList.EXPORTID;
                newRecord.ISITEMPAY = itemFindNewList.ISITEMPAY;
                newRecord.ITEMDISCOUNT = itemFindNewList.ITEMDISCOUNT;
                newRecord.ITEMID = itemFindNewList.ITEMID;
                newRecord.ITEMNAME = itemFindNewList.ITEMNAME;
                newRecord.ISITEMPAY = itemFindNewList.ISITEMPAY;
                newRecord.PRICE = itemFindNewList.PRICE;
                newRecord.QUANTITY = 1;
                newRecord.SERVICE = itemFindNewList.SERVICE;
                newRecord.TABLEID = itemFindNewList.TABLEID;
                newRecord.TABLENAME = itemFindNewList.TABLENAME;
                newRecord.TOTAL = itemFindNewList.TOTAL;
                newRecord.VAT = itemFindNewList.VAT;

                listExp.Add(newRecord);
                gridListItemOld.RefreshDataSource();
                return;
            }
        
        }

        
        private void gridListItemNew_DoubleClick(object sender, EventArgs e)
        {
            
            if ((e as DXMouseEventArgs).Button == System.Windows.Forms.MouseButtons.Right
             && (e as DXMouseEventArgs).Clicks == 2)
            {

                var itemFindNewList =
                    this.listFlag.Find(
                        f => f.ITEMID == int.Parse(gridView5.GetFocusedRowCellValue("ITEMID").ToString()));

                if (itemFindNewList == null)
                {

                    return;
                }
                var resultOld1 =
                    this.listExp.Find(f => f.ITEMID == int.Parse(gridView5.GetFocusedRowCellValue("ITEMID").ToString()));
                if (resultOld1 != null)
                {

                    if (itemFindNewList.QUANTITY == 1)
                    {
                        gridView5.DeleteRow(gridView5.GetFocusedDataSourceRowIndex());
                        gridListItemNew.RefreshDataSource();
                        resultOld1.QUANTITY++;

                        gridListItemOld.RefreshDataSource();
                        return;
                    }
                    itemFindNewList.QUANTITY--;
                    gridListItemNew.DataSource = listFlag;
                    gridListItemNew.RefreshDataSource();
                    resultOld1.QUANTITY++;
                    gridListItemOld.RefreshDataSource();
                    return;
                }
                var newRecord = new EXPORTDETAILENT();
                newRecord.AREANAME = itemFindNewList.AREANAME;
                newRecord.DATECREATE = itemFindNewList.DATECREATE;
                newRecord.DATEPAID = itemFindNewList.DATEPAID;
                newRecord.EMPLOYERID = itemFindNewList.EMPLOYERID;
                newRecord.EMPLOYERNAME = itemFindNewList.EMPLOYERNAME;
                newRecord.EXPORTID = itemFindNewList.EXPORTID;
                newRecord.ISITEMPAY = itemFindNewList.ISITEMPAY;
                newRecord.ITEMDISCOUNT = itemFindNewList.ITEMDISCOUNT;
                newRecord.ITEMID = itemFindNewList.ITEMID;
                newRecord.ITEMNAME = itemFindNewList.ITEMNAME;
                newRecord.ISITEMPAY = itemFindNewList.ISITEMPAY;
                newRecord.PRICE = itemFindNewList.PRICE;
                newRecord.QUANTITY = 1;
                newRecord.SERVICE = itemFindNewList.SERVICE;
                newRecord.TABLEID = itemFindNewList.TABLEID;
                newRecord.TABLENAME = itemFindNewList.TABLENAME;
                newRecord.TOTAL = itemFindNewList.TOTAL;
                newRecord.VAT = itemFindNewList.VAT;

                listExp.Add(newRecord);
                gridListItemOld.RefreshDataSource();
               


                if (itemFindNewList.QUANTITY == 1)
                {
                    gridView5.DeleteRow(gridView5.GetFocusedDataSourceRowIndex());
                    gridListItemNew.RefreshDataSource();
                  
                    gridListItemOld.RefreshDataSource();
                    return;
                }
                itemFindNewList.QUANTITY--;
                gridListItemNew.RefreshDataSource();
                return;
            }
        }

        private void btnSlipt_Click(object sender, EventArgs e)
        {

            try
            {
                if (int.Parse(glkTableNew.EditValue.ToString())== int.Parse(glkTableOld.EditValue.ToString()))
                {
                    MessageBox.Show("Vui lòng chọn 2 bàn khác nhau mới có thể tiến hành ghép được", "Thông báo");
                    return;
                }
                var expId = listExp[0].EXPORTID;

                foreach (var exportdetailent in listFlag)
                {
                    exportbll.ADDITEMTOTABLE(expId, exportdetailent.ITEMID, (int)(exportdetailent.QUANTITY));
                     exportbll.ADDITEMTOTABLE(exportdetailent.EXPORTID, exportdetailent.ITEMID, (int)-(exportdetailent.QUANTITY));
                
                }
                
                var table = int.Parse(glkTableOld.EditValue.ToString());
                general.tableIdJoin = int.Parse(glkTableNew.EditValue.ToString());
                if (table != null)
                {
                    general.TableId = table;
                }
               
                MessageBox.Show("Ghép bàn thành công", "Thông báo");
                this.Close();
            }
            catch (Exception)
            {
                    
                
            }

        }

        private void glkTableNew_EditValueChanged(object sender, EventArgs e)
        {
            try
            {

                EXPORTDETAILBLL bll = new EXPORTDETAILBLL();
                listFlag = bll.GETDETAILEXPORTBYTABLEID(int.Parse(glkTableNew.EditValue.ToString()));
                gridListItemNew.DataSource = listFlag;
                gridListItemNew.RefreshDataSource();

                if (gridView5.RowCount > 0)
                {
                    gridView5.FocusedRowHandle = gridView5.RowCount - 1;
                    gridView5.Focus();

                }
            }
            catch (Exception)
            {

                return;
            }
        }




    }
}